package com.ticket.controller;

import com.ticket.pojo.PageBean;
import com.ticket.pojo.User;
import com.ticket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public PageBean<User> selectByPage(@PathVariable int currentPage,@PathVariable int pageSize){
        System.out.println("user selectByPage is running ... ");
        PageBean<User> userPageBean = userService.selectByPage(currentPage, pageSize);
        System.out.println(userPageBean);
        return userPageBean;
    }

    @PostMapping("/{currentPage}/{pageSize}")
    PageBean<User> selectByPageAndCondition(@PathVariable int currentPage,@PathVariable int pageSize,@RequestBody User user){
        System.out.println("user selectByPageAndCondition is running ... ");
        System.out.println(user);
        PageBean<User> userPageBean = userService.selectByPageAndCondition(currentPage, pageSize, user);
        System.out.println(userPageBean);
        return userPageBean;
    }
    @PostMapping
    public String add(@RequestBody User user) {
        System.out.println("user add is running ... ");
        System.out.println(user);
        userService.add(user);
        return "success";
    }
    @PutMapping
    public String update(@RequestBody User user){
        System.out.println("user update is running ... ");
        System.out.println(user);
        userService.update(user);
        return "success";
    }
    @RequestMapping("/login")
    public int findUser(@RequestBody User user){
        System.out.println("user findUser is running ... ");
        System.out.println(user);
        return 0;
//        return userService.findUser(user);
    }
    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable int id){
        System.out.println("user deleteById is running ... ");
        System.out.println(id);
        userService.deleteById(id);
        return "success";
    }

    @DeleteMapping
    public String deleteByIds(@RequestBody int[] ids){
        System.out.println("user deleteByIds is running ... ");
        System.out.println(ids);
        userService.deleteByIds(ids);
        return "success";
    }
}
